'''
http://php.weather.sina.com.cn/xml.php?city=%C4%CF%B2%FD&password=DJOYnieT8234jlsK&day=0
其中的password是固定的，不要更改。
city为你要获取的城市，这里是获取南昌的天气，南昌对应的gb2312的编码为%C4%CF%B2%FD
'''
import requests
import re
from bs4 import BeautifulSoup


def string_to_urlbm(s):
    # 将字符串转化为gbk编码，得到b'\xc4\xcf\xb2\xfd'
    t = s.encode('gb2312')
    # 将得到的byte转化为字符串
    t = str(t)
    # 替换并且用，得到b'%c4%cf%b2%fd'
    s = t.replace('\\x', '%')
    rule = re.compile(r"b\'(.*)\'")
    # 用正则表达式提取并且大写，得到%C4%CF%B2%FD
    return rule.findall(s)[0].upper()



city = input('输入你想要查询的城市：')
city = string_to_urlbm(city)
try:
    req = requests.get('http://php.weather.sina.com.cn/xml.php?city=%s&password=DJOYnieT8234jlsK&day=0' % city)
    t = req.content.decode('utf-8')
    sj = BeautifulSoup(t, "html.parser")
    cy = sj.find('city').string
    status1 = sj.find('status1').string
    status2 = sj.find('status2').string
    temperature1 = sj.find('temperature1').string
    temperature2 = sj.find('temperature2').string
    print(cy,status1,temperature1)
except:
    print('输入城市有误')
